<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'History' );

$turn = getCurrentTurn();

if( isset( $_GET[ 'id' ] ) ) {
	$history_id = formatInput( $_GET[ 'id' ] );
    $query = "SELECT count(*) " .
    		"FROM history " .
    		"WHERE history_id = '$history_id' AND visible = 1 AND turn <= $turn";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( $row[ 'count(*)' ] == 0 ) {
		$history_id = -1;
	}
}
elseif( isset( $_GET[ 'date' ] ) ) {
    $query = "SELECT history_id " .
    		"FROM history " .
    		"WHERE date = '" . formatInput( $_GET[ 'date' ] ) . "' AND visible = 1 AND turn <= $turn";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'history_id' ] ) ) {
		$history_id = $row[ 'history_id' ];
	}
	else {
		$history_id = -1;
	}
}
else {
    $query = "SELECT history.history_id " .
    		"FROM history_seen " .
    		"JOIN history ON history.history_id = history_seen.history_id " .
    		"WHERE history_seen.ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND visible = 1 AND turn <= $turn AND seen = 0 " .
    		"ORDER BY turn ASC LIMIT 1";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'history_id' ] ) ) {
		$history_id = $row[ 'history_id' ];
	}
	else {
	    $query = "SELECT history_id " .
	    		"FROM history " .
	    		"WHERE visible = 1 AND turn <= $turn " .
	    		"ORDER BY turn DESC LIMIT 1";
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'history_id' ] ) ) {
			$history_id = $row[ 'history_id' ];
		}
		else {
			$history_id = -1;		
		}
	}
}

print_history_tab();

if( $history_id == -1 ) {
?>
					<div class="blok">
						<?php transprint( 'No history available or wrong link.' ); ?>
					</div> <!-- close blok -->
<?php
}
else {
    $query = "UPDATE history_seen " .
    		"SET seen = 1 " .
    		"WHERE history_id = " . $history_id . " AND ruler_id = " . $_SESSION[ 'ruler_id' ];
    mysql_query( $query ) or die( "Query failed : " . mysql_error() );
    
    $query = "SELECT * " .
    		"FROM history " .
    		"WHERE history_id = " . $history_id . " AND visible = 1 AND turn <= $turn";
    
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	
	$allow_comments = $row[ 'allow_comments' ];
	
    if( $row[ 'ruler_id' ] != -1 && $row[ 'anonymous' ] == 0 ) {
	    $query2 = "SELECT name " .
	    		"FROM ruler " .
	    		"WHERE ruler_id = " . $row[ 'ruler_id' ];
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$author = "<a title=\"" . translate( 'author stats' ) . "\" href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>";
    }
    else {
    	$author = translate( "World News Reporter" );
    }
    
    $writer_id = $row[ 'ruler_id' ];
?>
					<div class="blok">
						<h2><?php print( $row[ 'title' ] ); ?></h2><br />
<?php
	if( can_publish_history() ) {
		print( "(<a href=\"write.php?id=$history_id\">Edit</a>" );
		print( " | " );
		print( "<a href=\"revoke.php?id=$history_id\">Revoke</a>" );
		print( ")<br />" );
	}
	
	if( $writer_id != -1 ) {
	    $query2 = "SELECT SUM(history_rating.score) " .
	    		"FROM history_rating " .
	    		"JOIN history_tags ON history_tags.tag_id = history_rating.tag_id " .
	    		"WHERE history_id = $history_id AND positive >= negative";
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$total_score = $row2[ 'SUM(history_rating.score)' ];
	    $query2 = "SELECT SUM(history_rating.score) " .
	    		"FROM history_rating " .
	    		"JOIN history_tags ON history_tags.tag_id = history_rating.tag_id " .
	    		"WHERE history_id = $history_id AND positive < negative";
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$total_score -= $row2[ 'SUM(history_rating.score)' ];
		
		print( translate( "Rating" ) . ": " . number_format( round( $total_score ) ) );
		
	    $query2 = "SELECT tag " .
	    		"FROM history_tags " .
	    		"JOIN history_rating ON history_rating.tag_id = history_tags.tag_id " .
	    		"WHERE history_id = $history_id " .
	    		"ORDER BY history_rating.score DESC LIMIT 3";
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	    $num = mysql_numrows( $result2 );
		$used_tags = false;
		if( $num > 0 ) {
			$used_tags = true;
			print( " (" );
			$i = 0;
			
			while ( $i < $num ) {
				if( $i > 0 ) {
					print( ", " );
				}
				print( mysql_result( $result2, $i, "tag" ) );
				$i ++;
			}
			print( ")" );
		}
		print( "<br />" );
	}
?>
						<small><?php transprint( 'Date' ); ?>: <?php print( $row[ 'date' ] ); ?></small><br />
						<small><?php transprint( 'Author' ); ?>: <?php print( $author ); ?></small><br />
						<pre><?php
								print( wordwrap( stripslashes( $row[ 'text' ] ), 100, "\n" ) ); 
							?></pre>
<?php
    $query = "SELECT history_id " .
    		"FROM history " .
    		"WHERE visible = 1 AND turn <= $turn " .
    		"ORDER BY turn DESC";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
    $num = mysql_numrows( $result );
	$i = 0;
	while ( $i < $num ) {
		if( mysql_result( $result, $i, "history_id" ) == $history_id ) {
			if( $i < $num - 1 ) {
				print( "<div id=\"text_left\"><a href=\"history.php?id=" . mysql_result( $result, ( $i + 1 ), "history_id" ) . "\">" . translate( 'Previous' ) . "</a></div>\r\n" );
			}
			if( $i > 0 ) {
				print( "<div id=\"text_right\"><a href=\"history.php?id=" . mysql_result( $result, ( $i - 1 ), "history_id" ) . "\">" . translate( 'Next' ) . "</a></div><br />\r\n" );
			}
			else {
				print( "<br />\r\n" );
			}
		}
		$i ++;
	}
?>
					</div>
					<br />
<?php
	if( isset( $_SESSION[ 'history_rating_status' ] ) ) {
		print( "\t\t\t\t<div class=\"blok\">\r\n\t\t\t\t" . $_SESSION[ 'history_rating_status' ] . "\r\n\t\t\t\t</div><br />\r\n" );
		unset( $_SESSION[ 'history_rating_status' ] );
	}
	
	$query = "SELECT rating_points " .
    		"FROM users " .
    		"WHERE user_id = " . $_SESSION[ 'user_id' ];
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$rating_points = $row[ 'rating_points' ];
	if( $writer_id != -1 && $_SESSION[ 'ruler_id' ] != $writer_id && $rating_points > 0 ) {
		$score = $rating_points;
		if( $score > 5 ) {
			$score = 5;
		}
	    if( $used_tags ) {
		    $query = "SELECT history_tags.*, history_rating.score AS rating " .
		    		"FROM history_tags " .
		    		"JOIN history_rating ON history_rating.tag_id = history_tags.tag_id " .
		    		"WHERE history_id = $history_id " .
		    		"ORDER BY history_rating.score DESC";
	    }
	    else {
		    $query = "SELECT * " .
		    		"FROM history_tags " .
		    		"ORDER BY score DESC LIMIT 20";
	    }
	    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	    $num = mysql_numrows( $result );
		
		if( $used_tags ) {
			$first_selected = " selected=\"selected\"";
			$second_selected = "";
			$value = "item";
		}
		else {
			$first_selected = "";
			$second_selected = " selected=\"selected\"";
			$value = "top20";
		}
?>
					<div class="blok" id="tag-rating">
						<strong><?php transprint( 'Rating' ); ?></strong><br />
						<?php transprint( 'My rating points' ); ?>: <?php print( number_format( floor( $rating_points ) ) ); ?><br />
						<br />
						<strong><?php transprint( 'Select a tag from' ); ?>:</strong> <select id="tag-type-select" value="<?php print( $value ); ?>" onchange="loadNewTags()">
							<option value="item"<?php print( $first_selected ); ?>><?php transprint( 'tags used to rate this item' ); ?></option>
							<option value="top20"<?php print( $second_selected ); ?>><?php transprint( 'top 20 tags' ); ?></option>
							<option value="top50"><?php transprint( 'top 50 tags' ); ?></option>
							<option value="top100"><?php transprint( 'top 100 tags' ); ?></option>
						</select>
						<div class="hidden" id="history_id"><?php print( $history_id ); ?></div>
						<br />
						<div id="tag-container">
						<?php
		$i = 0;
		while ( $i < $num ) {
			if( $i > 0 ) {
				print( ", " );
			}
			
			if( mysql_result( $result, $i, "positive" ) >= mysql_result( $result, $i, "negative" ) ) {
				$positive = "true";
			}
			else {
				$positive = "false";
			}
			
			print( "<a href=\"javascript:void(null);\" onclick=\"selectTag( '" . mysql_result( $result, $i, "tag" ) . "', $positive )\">" . mysql_result( $result, $i, "tag" ) . "</a>" );
			if( $used_tags ) {
				$plus = "-";
				if( $positive == 'true' ) {
					$plus = "+";
				}
				print( " ($plus" . number_format( mysql_result( $result, $i, "rating" ) ) . ")" );
			}
			$i ++;
		}
						
						?>
						</div> <!-- close rating-container -->
						<br />
						<form method="post" action="submit_history_rating.php">
							<?php transprint( 'Tag' ); ?>: <input type="text" name="tag" id="tag-field" size="20" /><br />
							<?php transprint( 'Score' ); ?>: <input type="text" name="score" value="<?php print( floor( $score ) ); ?>" size="2" /><br />
							<?php transprint( 'Rating type' ); ?>:<br />
							<input id="positive-checkbox" type="radio" name="type" value="positive" checked="checked" /> <?php transprint( 'Positive' ); ?><br />
							<input id="negative-checkbox" type="radio" name="type" value="negative" /> <?php transprint( 'Negative' ); ?><br />
							<small>(<?php transprint( 'Is this type of rating positive or negative. Example: "good" is a positive rating and "bad" is a negative rating' ); ?>)</small><br />
							<input type="hidden" name="history_id" value="<?php print( $history_id ); ?>" /><input type="submit" name="submit score" value="<?php transprint( 'submit score' ); ?>" />
						</form>
					</div>
					<br />
<?php
	}
	
	if( $writer_id != -1 ) {
		include( 'comments.php' );
	}
}
?>
					<script type="text/javascript" src="history.js"></script>
<?php
aos_footer();
?>
